package com.mql.ganio.ui.presenter;

import android.text.TextUtils;

import com.mql.ganio.R;
import com.mql.ganio.ui.activity.GanHuoActivity;
import com.mql.ganio.ui.base.BaseActivity;
import com.mql.ganio.ui.base.BasePresenter;
import com.mql.ganio.ui.view.ILoginAtView;
import com.mql.ganio.util.LogUtils;
import com.mql.ganio.util.UIUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 作者：Administrator on 2017/11/7 12:01
 */
public class LoginAtPresenter extends BasePresenter<ILoginAtView> {
    public LoginAtPresenter(BaseActivity context) {
        super(context);
    }

    public void login() {
        String phone = getView().getPhone().getText().toString();
        String passWord = getView().getPassWord().getText().toString();
        if (TextUtils.isEmpty(phone)) {
            UIUtils.showToast(UIUtils.getString(R.string.phone_not_empty));
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            UIUtils.showToast(UIUtils.getString(R.string.password_not_empty));
            return;
        }

        mContext.showWaitingDialog(UIUtils.getString(R.string.loging));
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                mContext.jumpToActivity(GanHuoActivity.class);
                mContext.hideWaitingDialog();

            }
        };
        Timer timer = new Timer();
        timer.schedule(task, 3000);//3秒后执行TimeTask的run方法
//        ApiRetrofit.getInstance().login(phone, passWord)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<LoginResponse>() {
//                    @Override
//                    public void call(LoginResponse loginResponse) {
//                        int code = loginResponse.getCode();
//                        mContext.hideWaitingDialog();
//                        if (code == 0) {
//                            LogUtils.e("---->",loginResponse.getData().toString());
//                            UserCache.save(loginResponse.getData().getUserId(), loginResponse.getData().getPhoneNum(), loginResponse.getData().getRongToken());
//                            mContext.jumpToActivity(GanHuoActivity.class);
//                          //  mContext.finish();
//                        } else {
//                            loginError(new ServerException(UIUtils.getString(R.string.login_error) + code));
//                        }
//                    }
//                });
    }

    public void loginError(Throwable throwable) {
        LogUtils.e(throwable.getLocalizedMessage());
        UIUtils.showToast(throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }
}
